#########################################################################################
####################################  FIGURE 3   ########################################
#############  The Effects of Formal Leadership on Women’s Influence  ###################
#########################################################################################

# Apply necessary packages
my_packages <- c("ggplot2",
                 "haven",
                 "DescTools",
                 "readxl",
                 "plyr",
                 "dplyr",
                 "gridExtra",
                 "ggpubr",
                 "rstatix",
                 "tidyverse")
lapply(my_packages, require, character.only = TRUE)

rm(list=ls())

# Change working directory
#setwd("C:/Users/ganth/Dropbox/StrengthInNumbersReplicationPackage/replicable/figure_3")

#########################################################################################


###Figure 3a###

# Import Study 2 Task 1 analysis produced in STATA do file
task1_minfl <- read.csv("intermediate_data/study2_newfig_groupgenderminfl_ratio_r1_forgraphing.csv") %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
task1_minflp <- read_excel("intermediate_data/study2_newfig_groupgenderminfl_ratio_r1p.xlsx")
task1_minflp$p <- as.character(task1_minflp$p)
task1_minflp$p[task1_minflp$p == '0'] <- '<0.001'

# Import Study 2 Task 2 (Male Leader) analysis produced in STATA do file
task2_minfl_m <- read.csv("intermediate_data/study2_newfig_groupgenderminfl_ratio_r2_male_forgraphing.csv") %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
task2_minfl_mp <- read_excel("intermediate_data/study2_newfig_groupgenderminfl_ratio_r2_malep.xlsx")
task2_minfl_mp$p <- as.character(task2_minfl_mp$p)
task2_minfl_mp$p[task2_minfl_mp$p == '0'] <- '<0.001'

# Import Study 2 Task 2 (Female Leader) analysis produced in STATA do file
task2_minfl_f <- read.csv("intermediate_data/study2_newfig_groupgenderminfl_ratio_r2_fem_forgraphing.csv") %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  mutate(v5 = factor(v5, levels=c("Majority Male (L)", "Majority Female (L)", "Majority Male", "Majority Female"))) %>%
  arrange(v1,v5)
task2_minfl_fp <- read_excel("intermediate_data/study2_newfig_groupgenderminfl_ratio_r2_femp.xlsx")
task2_minfl_fp$p <- as.character(task2_minfl_fp$p)
task2_minfl_fp$p[task2_minfl_fp$p == '0'] <- '<0.001'

# Initiate figure file
png(filename = "figure_3a.png", width = 1300, height = 800)

# Study 2 Task 1 Most Influential Figure
task1_minflvotes <- ggplot(task1_minfl, aes(x=v1, y=v2), fill="dodgerblue3") +
  geom_bar(data=task1_minfl, stat="identity", aes(x=v1, y=v2), colour="black", fill = c("dodgerblue3", "darkgoldenrod2"), size=.3, width = .55) +
  geom_errorbar(data=task1_minfl, aes(ymin=v2-ci, ymax=v2+ci), linewidth=0.8, width=.1, position=position_dodge(.9)) +
  xlab("Task 1") +
  ylab("Votes per woman") +
  theme(text=element_text(size=13),
        plot.title = element_text(hjust=.5),
        axis.title.y=element_text(size=20),
        axis.title.x=element_text(size=16),
        axis.text.x=element_text(size=12),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour="black")) +
  geom_hline(yintercept = 1, linetype="dashed") +
  coord_cartesian(ylim = c(0, 1.5))
task1_minflvotes <- task1_minflvotes + stat_pvalue_manual(task1_minflp, label = "p={p}", size=5)

# Study 2 Task 2 (Female Leader) Most Influential Figure
task2_minflvotes_f <- ggplot(task2_minfl_f, aes(x=v1, y=v6), fill="dodgerblue3") +
  geom_bar(data=task2_minfl_f, stat="identity", aes(x=v1, y=v6), colour="black", fill = c("dodgerblue3", "darkgoldenrod2"), size=.3, width = .55) +
  geom_errorbar(data=task2_minfl_f, aes(ymin=v6-ci, ymax=v6+ci), linewidth=0.8, width=.1, position=position_dodge(.9)) +
  xlab("Task 2 (Female Leader)") +
  theme(text=element_text(size=13),
        plot.title = element_text(hjust=.5),
        axis.title=element_text(size=16),
        axis.text.x=element_text(size=12),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.line = element_line(colour="black")) +
  geom_hline(yintercept = 1, linetype="dashed") +
  coord_cartesian(ylim = c(0, 1.5))
task2_minflvotes_f <- task2_minflvotes_f + stat_pvalue_manual(task2_minfl_fp, label = "p={p}", size=5)

# Study 2 Task 2 (Male Leader) Most Influential Figure
task2_minflvotes_m <- ggplot(task2_minfl_m, aes(x=v1, y=v2), fill="dodgerblue3") +
  geom_bar(data=task2_minfl_m, stat="identity", aes(x=v1, y=v2), colour="black", fill = c("dodgerblue3", "darkgoldenrod2"), size=.3, width = .55) +
  geom_errorbar(data=task2_minfl_m, aes(ymin=v2-ci, ymax=v2+ci), linewidth=0.8, width=.1, position=position_dodge(.9)) +
  xlab("Task 2 (Male Leader)") +
  theme(text=element_text(size=18),
        plot.title = element_text(hjust=.5),
        axis.title=element_text(size=16),
        axis.text.x=element_text(size=12),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.line.y = element_blank(),
        axis.line.x = element_line(colour="black")) +
  geom_hline(yintercept = 1, linetype="dashed") +
  coord_cartesian(ylim = c(0, 1.5))
task2_minflvotes_m <- task2_minflvotes_m + stat_pvalue_manual(task2_minfl_mp, label = "p={p}", size=5)

# Combine figures and write to file
inflvotes <- grid.arrange(task1_minflvotes, task2_minflvotes_f, task2_minflvotes_m, nrow = 1)
inflvotes
dev.off()

###Figure 3b###

# Import Study 2 Task 1 analysis produced in STATA do file
task1_spokes <- read.csv("intermediate_data/study2_newfig_groupgenderspokes_ratio_r1_forgraphing.csv") %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
task1_spokesp <- read_excel("intermediate_data/study2_newfig_groupgenderspokes_ratio_r1p.xlsx")
task1_spokesp$p <- as.character(task1_spokesp$p)
task1_spokesp$p[task1_spokesp$p == '0'] <- '<0.001'

# Import Study 2 Task 2 (Male Leader) analysis produced in STATA do file
task2_spokes_m <- read.csv("intermediate_data/study2_newfig_groupgenderspokes_ratio_r2_male_forgraphing.csv") %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
task2_spokes_mp <- read_excel("intermediate_data/study2_newfig_groupgenderspokes_ratio_r2_malep.xlsx")
task2_spokes_mp$p <- as.character(task2_spokes_mp$p)
task2_spokes_mp$p[task2_spokes_mp$p == '0'] <- '<0.001'

# Import Study 2 Task 2 (Female Leader) analysis produced in STATA do file
task2_spokes_f <- read.csv("intermediate_data/study2_newfig_groupgenderspokes_ratio_r2_fem_forgraphing.csv") %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  mutate(v5 = factor(v5, levels=c("Majority Male (L)", "Majority Female (L)", "Majority Male", "Majority Female"))) %>%
  arrange(v1,v5)
task2_spokes_fp <- read_excel("intermediate_data/study2_newfig_groupgenderspokes_ratio_r2_femp.xlsx")
task2_spokes_fp$p <- as.character(task2_spokes_fp$p)
task2_spokes_fp$p[task2_spokes_fp$p == '0'] <- '<0.001'

# Initiate figure file
png(filename = "figure_3b.png", width = 1300, height = 800)

# Study 2 Task 1 Most Influential Figure
task1_spokesvotes <- ggplot(task1_spokes, aes(x=v1, y=v2), fill="dodgerblue3") +
  geom_bar(data=task1_spokes, stat="identity", aes(x=v1, y=v2), colour="black", fill = c("dodgerblue3", "darkgoldenrod2"), size=.3, width = .55) +
  geom_errorbar(data=task1_spokes, aes(ymin=v2-ci, ymax=v2+ci), linewidth=0.8, width=.1, position=position_dodge(.9)) +
  xlab("Task 1") +
  ylab("Votes per woman") +
  theme(text=element_text(size=13),
        plot.title = element_text(hjust=.5),
        axis.title.y=element_text(size=20),
        axis.title.x=element_text(size=16),
        axis.text.x=element_text(size=12),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour="black")) +
  geom_hline(yintercept = 1, linetype="dashed") +
  coord_cartesian(ylim = c(0, 1.5))
task1_spokesvotes <- task1_spokesvotes + stat_pvalue_manual(task1_spokesp, label = "p={p}", size=5)

# Study 2 Task 2 (Female Leader) Most Influential Figure
task2_spokesvotes_f <- ggplot(task2_spokes_f, aes(x=v1, y=v6), fill="dodgerblue3") +
  geom_bar(data=task2_spokes_f, stat="identity", aes(x=v1, y=v6), colour="black", fill = c("dodgerblue3", "darkgoldenrod2"), size=.3, width = .55) +
  geom_errorbar(data=task2_spokes_f, aes(ymin=v6-ci, ymax=v6+ci), linewidth=0.8, width=.1, position=position_dodge(.9)) +
  xlab("Task 2 (Female Leader)") +
  theme(text=element_text(size=13),
        plot.title = element_text(hjust=.5),
        axis.title=element_text(size=16),
        axis.text.x=element_text(size=12),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.line = element_line(colour="black")) +
  geom_hline(yintercept = 1, linetype="dashed") +
  coord_cartesian(ylim = c(0, 1.5))
task2_spokesvotes_f <- task2_spokesvotes_f + stat_pvalue_manual(task2_spokes_fp, label = "p={p}", size=5)

# Study 2 Task 2 (Male Leader) Most Influential Figure
task2_spokesvotes_m <- ggplot(task2_spokes_m, aes(x=v1, y=v2), fill="dodgerblue3") +
  geom_bar(data=task2_spokes_m, stat="identity", aes(x=v1, y=v2), colour="black", fill = c("dodgerblue3", "darkgoldenrod2"), size=.3, width = .55) +
  geom_errorbar(data=task2_spokes_m, aes(ymin=v2-ci, ymax=v2+ci), linewidth=0.8, width=.1, position=position_dodge(.9)) +
  xlab("Task 2 (Male Leader)") +
  theme(text=element_text(size=18),
        plot.title = element_text(hjust=.5),
        axis.title=element_text(size=16),
        axis.text.x=element_text(size=12),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.line.y = element_blank(),
        axis.line.x = element_line(colour="black")) +
  geom_hline(yintercept = 1, linetype="dashed") +
  coord_cartesian(ylim = c(0, 1.5))
task2_spokesvotes_m <- task2_spokesvotes_m + stat_pvalue_manual(task2_spokes_mp, label = "p={p}", size=5)

# Combine figures and write to file
spokesvotes <- grid.arrange(task1_spokesvotes, task2_spokesvotes_f, task2_spokesvotes_m, nrow = 1)
spokesvotes
dev.off()


